import { Controller } from "../../mvc/core/Controller";
import { GuideTipsManager } from "../manager/GuideTipsManager";
import { ItemData } from "../resource/info/ItemData";
import { GuideFragmentUseCache } from "../cache/GuideFragmentUseCache";
import { GameController } from "../mvc/GameController";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { DataEvent } from "../events/DataEvent";
import { EventName } from "../mvc/EventName";
import { IView } from "../../mvc/interfaces/IView";
//class GuideFragmentUseController
    
    export  class GuideFragmentUseController extends Controller
    {
       

        /* internal  */startGuideTime(): void
        {
            this._timeKey = flash.utils.setTimeout(this.onGuideTimeOut, cache.guideFragmentUse.guideTime);
            return;
        }

        /* internal  */onGuideTimeOut(): void
        {
            GuideTipsManager.hideGuideTips();
            cache.guideFragmentUse.guideStep = -1;
            return;
        }

        /* internal  */guideOpenPack(arg1: ItemData): void
        {
            flash.utils.clearTimeout(this._timeKey);
            cache.guideFragmentUse.guideStep = GuideFragmentUseCache.GuideStep_OpenPack;
            if (GameController.pack.view.isHide) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.Guide_FragmentUseOpenPack, arg1));
                Dispatcher.addEventListener(EventName.PackWindowShow, this.onPackWindowShow);
            }
            else 
            {
                this.guideItemEffect();
            }
            return;
        }

        /* internal  */guideItemEffect(): void
        {
            GuideTipsManager.hideGuideTips();
            flash.utils.clearTimeout(this._timeKey);
            cache.guideFragmentUse.guideStep = GuideFragmentUseCache.GuideStpe_ItemEffect;
            Dispatcher.dispatchEvent(new DataEvent(EventName.BackPackCodeItemPlayEffect, cache.guideFragmentUse.guideItem.itemCode));
            if (GameController.player.isModeSelfOpen()) 
            {
                this.guideExchangeTab();
            }
            else 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.Guide_FragmentUseOpenRole));
                Dispatcher.addEventListener(EventName.PlayerWindowShow, this.onPlayerWindowShow);
            }
            return;
        }

        /* internal  */guideExchangeTab(): void
        {
            GuideTipsManager.hideGuideTips();
            flash.utils.clearTimeout(this._timeKey);
            cache.guideFragmentUse.guideStep = GuideFragmentUseCache.GuideStep_ExchangeTab;
            Dispatcher.dispatchEvent(new DataEvent(EventName.Guide_FragmentUseExchangeTab));
            Dispatcher.addEventListener(EventName.PlayerTabBarChangeToExchange, this.onPlayerTabBarChangeToExchangeHandler);
            return;
        }

        /* internal  */guideExchangeOpt(): void
        {
            GuideTipsManager.hideGuideTips();
            flash.utils.clearTimeout(this._timeKey);
            cache.guideFragmentUse.guideStep = GuideFragmentUseCache.GuideStep_ExchangeOpt;
            Dispatcher.dispatchEvent(new DataEvent(EventName.Guide_FragmentUseExchangeOpt));
            return;
        }

        /* internal  */onGuideFragemengUseHandler(arg1: DataEvent): void
        {
            this.guideOpenPack(arg1.data as ItemData);
            this.startGuideTime();
            return;
        }

        /* internal  */onPackWindowShow(arg1: DataEvent): void
        {
            Dispatcher.removeEventListener(EventName.PackWindowShow, this.onPackWindowShow);
            if (cache.guideFragmentUse.isGuideOpenPack()) 
            {
                this.guideItemEffect();
            }
            return;
        }

        /* internal  */onPlayerWindowShow(arg1: DataEvent): void
        {
            if (GameController.player.isModeSelfOpen()) 
            {
                Dispatcher.removeEventListener(EventName.PlayerWindowShow, this.onPlayerWindowShow);
                if (cache.guideFragmentUse.isGuideOpenPlayer()) 
                {
                    this.guideExchangeTab();
                }
            }
            return;
        }

        /* internal  */onPlayerTabBarChangeToExchangeHandler(arg1: DataEvent): void
        {
            Dispatcher.removeEventListener(EventName.PlayerTabBarChangeToExchange, this.onPlayerTabBarChangeToExchangeHandler);
            if (cache.guideFragmentUse.isGuideExchangeTab()) 
            {
                this.guideExchangeOpt();
            }
            return;
        }

        protected /* override */ initServer(): void
        {
            super.initServer();
            Dispatcher.addEventListener(EventName.Guide_FragmentUse, this.onGuideFragemengUseHandler);
            return;
        }

        protected /* override */ initView(): IView
        {
            return super.initView();
        }

        private /* var */_timeKey: uint;
    }
